package com.gitee.huanminabc.jtimerwheel.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gitee.huanminabc.jcommon.str.StringCalculateUtil;
import com.gitee.huanminabc.jcommon.str.StringIdentifyUtil;
import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.jtimerwheel.mode.TimeBeginMode;
import com.gitee.huanminabc.jtimerwheel.mode.TimeEndMode;
import com.gitee.huanminabc.jtimerwheel.mode.TimePointMode;
import com.gitee.huanminabc.jtimerwheel.utils.TimerWheelDateEnum;
import com.gitee.huanminabc.jtimerwheel.utils.TimerWheelDateUtil;
import lombok.Data;

import java.util.*;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2022/11/30 17:07
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 * 语法格式
 * 语法: 0,0,7,0,0,0,0,- 长度为8
 * 对应时间: 秒,分,时,天,星期,月,年,执行计划
 * <p>
 * 执行计划
 * 数值: 执行几次
 * n: 循环执行
 * ^n: 时间乘阶 2*2,4*2,8*2....
 * ^数值: 时间乘阶几次 ^2 那么就是2*2,4*2
 * mode: 循环模式
 * mode数值: 模式执行几次
 * <p>
 * 符号
 * ~: 区间 1~4 那么就会执行1,2,3,4
 * /: 间隔 2/5 那么从2开始下次之后就从5开始执行
 * []: 分段 [5,10,20]  就是在当前时间  n+5  n+5+10   n+5+10+20
 * <p>
 * 模式
 * 模式就是可以: 月底,月初,每周几,星期末,年末,节假日等,(可以支持自定义扩张需要对外提供生成器)
 * <p>
 * 执行计划: n(循环执行) ,n数值(有限执行)
 * begin: 时间初(天初,周初,月初,年初)
 * end: 时间底(天底,周末,月底,年底)
 * point(?): 指定时间点,指定每月5号,每周3,每2天
 * <p>
 * 以上是基础模式,其他模式需要自定义(节假日,特殊日期等)
 * 注意: 在模式的基础上还可以配合时间比如: (每天2点1分10秒执行)
 * 案例可以参考: c语言-自研定时器计划任务语法   csdn
 */
@Data
public class TimerResolverCore {
    public TimeSlotInfo second;//秒 0-59
    public TimeSlotInfo minute;//分钟 0-59
    public TimeSlotInfo hour; //小时0-23
    public TimeSlotInfo day;  //天 1-31
    public TimeSlotInfo week;//星期 0-6
    public TimeSlotInfo month;//月份 1-12
    public TimeSlotInfo year;//年
    public String plan;//执行计划 -代表不执行,n 循环执行,数字代表执行次数 , ^n 阶级执行(^1 表示一阶执行,^2 表示二阶执行,^3 表示三阶执行...^n表示n阶执行)  mode 代表执行模式 mode2 代表模式执行2次....
    public String planRaw;// plan 的原始值,不会被修改
    public int timestamp;//时间戳 用于计算 秒级别
    public Map<String, Object> mode;//模式


    public static boolean mode_exist(TimeSlotInfo item, TimerResolverCore timerResolver) {
        if (!timerResolver.plan.contains("mode")) {
            return false;
        }
        Set<Map.Entry<String, Object>> entries = timerResolver.mode.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            if (item.getData().startsWith(entry.getKey())) {//如果匹配到模式,那么就转换为统一结构化格式
                return true;
            }
        }
        return false;
    }


    //模式解析
    public static void mode_timer(TimeSlotInfo item, int seat, TimerResolverCore timerResolver) {
        if (timerResolver.plan.contains("mode")) {
            Set<Map.Entry<String, Object>> entries = timerResolver.mode.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                if (item.getData().startsWith(entry.getKey())) {//如果匹配到模式
                    TimerWheelModeMethod pVoid = (TimerWheelModeMethod) entry.getValue();
                    pVoid.modeMethod(item, seat, timerResolver);//执行模式方法
                }
            }
        }
    }

    //添加模式方法(如果执行计划为mode,则支持)
    public static void add_timer_mode(TimerResolverCore timerResolver, String key, TimerWheelModeMethod method) {
        if (timerResolver.plan.contains("mode")) {
            timerResolver.mode.put(key, method);
        }
    }

    public static void inside_add_timer_mode(TimerResolverCore timerResolver) {
        if (timerResolver.plan.contains("mode") && timerResolver.mode.isEmpty()) {
            add_timer_mode(timerResolver, "begin", new TimeBeginMode());
            add_timer_mode(timerResolver, "end", new TimeEndMode());
            add_timer_mode(timerResolver, "point", new TimePointMode());
        }
    }

    //获取下一次执行时间,返回时间戳
    public static int get_next_time(TimerResolverCore timerResolver) {
        if (timerResolver.plan == null) {
            return 0;
        }
        //如果是模式,那么把每个槽位都进行模式解析一遍
        if (timerResolver.plan.contains("mode")) {
            mode_timer(get_return_data(timerResolver, "second"), 0, timerResolver);
            mode_timer(get_return_data(timerResolver, "minute"), 1, timerResolver);
            mode_timer(get_return_data(timerResolver, "hour"), 2, timerResolver);
            mode_timer(get_return_data(timerResolver, "day"), 3, timerResolver);
            mode_timer(get_return_data(timerResolver, "week"), 4, timerResolver);
            mode_timer(get_return_data(timerResolver, "month"), 5, timerResolver);
            mode_timer(get_return_data(timerResolver, "year"), 6, timerResolver);
        }
        //获取当前时间
        int second = get_return_data(timerResolver, "second").getReturnData();
        int minute =get_return_data(timerResolver, "minute").getReturnData();
        int hour = get_return_data(timerResolver, "hour").getReturnData();
        int day = get_return_data(timerResolver, "day").getReturnData();
        int week = get_return_data(timerResolver, "week").getReturnData();
        int month = get_return_data(timerResolver, "month").getReturnData();
        int year = get_return_data(timerResolver, "year").getReturnData();

        int newCurrent = TimerWheelDateUtil.addDateTimestamp(timerResolver.timestamp,
                second,
                minute,
                hour,
                day,
                month,
                year,
                week
        );
        timerResolver.timestamp = newCurrent;
        return newCurrent;
    }


    //解析字符串-,-,-,-,-,-,-,-并创建TimerResolver
    public static TimerResolverCore create_timer_resolver(String time) throws TimerWheelException {

        if (time == null || time.isEmpty()) {
            return null;
        }
        //去空
        time = time.trim();
        //排除[]内的,号
        int start = 0;
        while ((start = StringUtil.findRange(time, "[", start, time.length())) != -1) {
            int end = StringUtil.findRange(time, "]", start, time.length());
            time = StringUtil.replaceRangeAll(time, ",", "@", start, end);
            start++;
        }

        TimerResolverCore timerResolver = new TimerResolverCore();

        String[] pCharlist = time.split(",");
        int str_len = pCharlist.length;
        if (str_len != 8) {
            System.out.println("字符串格式错误,长度不为8,长度为:" + str_len);
            throw new TimerWheelException("create_timer_resolver语法错误: " + time);
        }
        timerResolver.second = new TimeSlotInfo(pCharlist[0]);
        timerResolver.minute = new TimeSlotInfo(pCharlist[1]);
        timerResolver.hour = new TimeSlotInfo(pCharlist[2]);
        timerResolver.day = new TimeSlotInfo(pCharlist[3]);
        timerResolver.week = new TimeSlotInfo(pCharlist[4]);
        timerResolver.month = new TimeSlotInfo(pCharlist[5]);
        timerResolver.year = new TimeSlotInfo(pCharlist[6]);
        timerResolver.timestamp = TimerWheelDateUtil.getDateTimeSecond();//获取当前时间戳
        //执行计划 -代表不执行,n 循环执行,数字代表执行次数 , ^n 阶级执行(^1 表示一阶执行,^2 表示二阶执行,^3 表示三阶执行...^n表示n阶执行)
        timerResolver.plan = "-".equals(pCharlist[7]) ? "0" : pCharlist[7];
        timerResolver.planRaw = timerResolver.plan;
        prefixPlanTrim(timerResolver);

        timerResolver.mode = new HashMap<>(20);
        //添加模式方法
        inside_add_timer_mode(timerResolver);
        //转换为结构化格式
        switchStructure(timerResolver);
        return timerResolver;
    }

    private static void prefixPlanTrim(TimerResolverCore timerResolver) {
        //如果是"0"就不执行
        if ("0".equals(timerResolver.plan)) {
            return;
        }
        //添加执行的次数
        if (!"n".equals(timerResolver.plan) && !"^".equals(timerResolver.plan)) {
            //如果开头是n或者^,那么就就去掉
            if (timerResolver.plan.startsWith("n")|| timerResolver.plan.startsWith("^")) {
                timerResolver.plan = timerResolver.plan.substring(1);
            }
            //如果开头是mode,那么就去掉
            if (timerResolver.plan.startsWith("mode")) {
                timerResolver.plan = timerResolver.plan.substring(4);
            }
        }
    }

    //打印TimerResolver,格式为-,-,-,-,-,-,-,-
    public void print_timer_resolver(TimerResolverCore timerResolver) {
        String format = String.format("%s,%s,%s,%s,%s,%s,%s,%s\n", timerResolver.second, timerResolver.minute, timerResolver.hour,
                timerResolver.day, timerResolver.week, timerResolver.month, timerResolver.year,
                timerResolver.plan);
        System.out.println(format);

    }

    //执行n次定时器并且打印效果
    public static void print_format_resolver(TimerResolverCore timerResolver, int n) throws TimerWheelException {
       //当前时间
        Date date = new Date();
        String currentDate = TimerWheelDateUtil.dateTurnString(date, TimerWheelDateEnum.DATETIME_PATTERN);
        System.out.println( "   当前时间: "+date.getTime()/1000+"(时间戳)---: " + currentDate+"(格式化时间)\n");
        for (int i = 0; i < n; ++i) {
            long i1 = resolver(timerResolver);
            String string = TimerWheelDateUtil.dateTurnString(TimerWheelDateUtil.secondTurnDate(i1), TimerWheelDateEnum.DATETIME_PATTERN);
            System.out.println("预计执行时间: " + i1 + "(时间戳)---" + string + "(格式化时间)\n");
        }
    }

    //解析定时器语法,并且生成下次执行时间,如果返回0,则此语法已经执行结束了,可以删除对应的定时器了
    public static int resolver(TimerResolverCore timerResolver) throws TimerWheelException {
        //不执行
        if ("0".equals(timerResolver.plan)) {
            return 0;
        }
        //判断是否可执行
        if ("n".equals(timerResolver.planRaw)) {//循环执行
            return get_next_time(timerResolver);
        }
        //阶级执行
        if (timerResolver.planRaw.startsWith("^")) {
            TimeSlotInfo pMapSecond = timerResolver.second;
            pMapSecond.setReturnData(get_return_data(timerResolver, "second").getReturnData() * 2);

            TimeSlotInfo pMapMinute = timerResolver.minute;
            pMapMinute.setReturnData(get_return_data(timerResolver, "minute").getReturnData()* 2);


            TimeSlotInfo pMapHour = timerResolver.hour;
            pMapHour.setReturnData(get_return_data(timerResolver, "hour").getReturnData() * 2 );


            TimeSlotInfo pMapDay = timerResolver.day;
            pMapDay.setReturnData(get_return_data(timerResolver, "day").getReturnData() * 2);


            TimeSlotInfo pMapWeek = timerResolver.week;
            pMapWeek.setReturnData(get_return_data(timerResolver, "week").getReturnData() * 2);


            TimeSlotInfo pMapMonth = timerResolver.month;
            pMapMonth.setReturnData(get_return_data(timerResolver, "month").getReturnData() * 2);


            TimeSlotInfo pMapYear = timerResolver.year;
            pMapYear.setReturnData(get_return_data(timerResolver, "year").getReturnData() * 2);


            if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {//如果是数字,那么就是有限阶级执行
                if ("0".equals(timerResolver.plan)) {
                    return 0;
                } else {
                    timerResolver.plan =  StringCalculateUtil.strCalculateOne(timerResolver.plan, "--");
                    return get_next_time(timerResolver);
                }
            }
        }
        //如果是mode模式
        if (timerResolver.plan != null && timerResolver.plan.startsWith("mode")) {
            if (timerResolver.plan.length() == 4) { //循环执行
                return get_next_time(timerResolver);
            } else {
                String string = timerResolver.plan.substring(4);
                if (StringIdentifyUtil.isNumeric(string)) {//如果是数字,那么就是有限阶级执行
                    if (string.equals("0")) {
                        return 0;
                    } else {
                        String one = StringCalculateUtil.strCalculateOne(string, "--");
                        timerResolver.plan = "mode".concat(one);
                        return get_next_time(timerResolver);
                    }
                } else {

                    throw new TimerWheelException(String.format("mode解析错误非法字符(必须是mode+数字):%s\n", timerResolver.plan));
                }
            }
        }

        if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {
            int plan = Integer.parseInt(timerResolver.plan);
            if (plan > 0) {
                timerResolver.plan = String.valueOf(plan - 1);
                return get_next_time(timerResolver);
            }
        }
        return 0;
    }

    public static void setTimerResolverField(TimerResolverCore timerResolver, String field, TimeSlotInfo timeSlotInfo) {
        if ("second".equals(field)) {
            timerResolver.second = timeSlotInfo;
        } else if ("minute".equals(field)) {
            timerResolver.minute = timeSlotInfo;
        } else if ("hour".equals(field)) {
            timerResolver.hour = timeSlotInfo;
        } else if ("day".equals(field)) {
            timerResolver.day = timeSlotInfo;
        } else if ("week".equals(field)) {
            timerResolver.week = timeSlotInfo;
        } else if ("month".equals(field)) {
            timerResolver.month = timeSlotInfo;
        } else if ("year".equals(field)) {
            timerResolver.year = timeSlotInfo;
        } else {
            throw new TimerWheelException("setTimerResolverField方法错误,没有找到对应的字段: " + field);
        }
    }

    public static TimeSlotInfo getTimerResolverField(TimerResolverCore timerResolver, String field) {
        if ("second".equals(field)) {
            return timerResolver.second;
        } else if ("minute".equals(field)) {
            return timerResolver.minute;
        } else if ("hour".equals(field)) {
            return timerResolver.hour;
        } else if ("day".equals(field)) {
            return timerResolver.day;
        } else if ("week".equals(field)) {
            return timerResolver.week;
        } else if ("month".equals(field)) {
            return timerResolver.month;
        } else if ("year".equals(field)) {
            return timerResolver.year;
        } else {
            throw new TimerWheelException("getTimerResolverField方法错误,没有找到对应的字段: " + field);
        }
    }

    //简易版的校验是否是json格式,用于在某些必然是json场景用
    private static boolean isJsonSimple(String json) {
        return json.startsWith("{") && json.endsWith("}") || json.startsWith("[") && json.endsWith("]");
    }

    //获取结构化数据的返回数据
    static TimeSlotInfo get_return_data(TimerResolverCore timerResolver, String field) {
        TimeSlotInfo pMap = getTimerResolverField(timerResolver, field);
        //获取类型
        String type = pMap.getType();
        //获取data
        String data = pMap.getData();
        //获取index
        Integer index = pMap.getIndex();
        //如果是[]类型 那么就是分段  例如 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
        //如果类型是~ 那么就是区间 1~5 表示[1,2,3,4,5]
        if (type.contains("[]") || type.contains("~")) {
            //获取数组
            JSONArray jsonArray = JSONArray.parseArray(data);
            //获取下一个值
            int nextIndex = index;
            //判断是否超出数组长度,如果超出长度,从头开始
            if (nextIndex >= jsonArray.size()) {
                //超出长度,返回空
                nextIndex = 0;
            }
            //获取下一个值
            Integer nextValue = jsonArray.getInteger(nextIndex);
            //设置返回数据
            pMap.setReturnData(nextValue);
            //设置下一个index
            pMap.setIndex(nextIndex + 1);
            //返回数据
            return pMap;
        } else if (type.contains("/")) { // 如果类型是/ (5/2)表示从5开始,每隔2个数 执行一次
            //如果类型是/
            //获取下一个值
            int nextIndex = index + 1;
            if (nextIndex == 1) {
                //获取数组
                JSONArray jsonArray = JSONArray.parseArray(data);
                //判断index是否是1,如果是1,取第二个值,并且转换为普通结构
                TimeSlotInfo timeSlotInfo = jsonArray.getObject(nextIndex, TimeSlotInfo.class);
                timeSlotInfo.setType("number");
                setTimerResolverField(timerResolver, field, timeSlotInfo);
            }
            return pMap;
        } else if (type.contains("mode")) {
            //获取返回数据
            Integer returnData = pMap.getReturnData();
            //如果类型是number,那么获取return,如果是null,那么就返回0
            pMap.setReturnData(returnData == null ? 0 : returnData);
            return pMap;
        } else if (type.contains("number")) {
            //获取返回数据
            return pMap;
        } else {
            throw new TimerWheelException("get_return_data方法错误,没有找到对应的类型: " + type);
        }
    }


    //定时器语法转换为结构化格式
    static void switchStructure(TimerResolverCore timerResolver) throws TimerWheelException {
        //秒
        if (timerResolver.second.getData().contains("[")) {
            section_analysis(timerResolver.second, timerResolver);
        } else if (timerResolver.second.getData().contains("~")) {
            range_analysis(timerResolver.second, timerResolver);
        } else if (timerResolver.second.getData().contains("/")) {
            interval_analysis(timerResolver.second, timerResolver);
        } else if (mode_exist(timerResolver.second, timerResolver)) {
            mode_analysis(timerResolver.second, timerResolver);
        } else {
            normal_analysis(timerResolver.second);
        }
        //分
        if (timerResolver.minute.getData().contains("[")) {
            section_analysis(timerResolver.minute, timerResolver);
        } else if (timerResolver.minute.getData().contains("~")) {
            range_analysis(timerResolver.minute, timerResolver);
        } else if (timerResolver.minute.getData().contains("/")) {
            interval_analysis(timerResolver.minute, timerResolver);
        } else if (mode_exist(timerResolver.minute, timerResolver)) {
            mode_analysis(timerResolver.minute, timerResolver);
        } else {
            normal_analysis(timerResolver.minute);
        }
        //时
        if (timerResolver.hour.getData().contains("[")) {
            section_analysis(timerResolver.hour, timerResolver);
        } else if (timerResolver.hour.getData().contains("~")) {
            range_analysis(timerResolver.hour, timerResolver);
        } else if (timerResolver.hour.getData().contains("/")) {
            interval_analysis(timerResolver.hour, timerResolver);
        } else if (mode_exist(timerResolver.hour, timerResolver)) {
            mode_analysis(timerResolver.hour, timerResolver);
        } else {
            normal_analysis(timerResolver.hour);
        }
        //日
        if (timerResolver.day.getData().contains("[")) {
            section_analysis(timerResolver.day, timerResolver);
        } else if (timerResolver.day.getData().contains("~")) {
            range_analysis(timerResolver.day, timerResolver);
        } else if (timerResolver.day.getData().contains("/")) {
            interval_analysis(timerResolver.day, timerResolver);
        } else if (mode_exist(timerResolver.day, timerResolver)) {
            mode_analysis(timerResolver.day, timerResolver);
        } else {
            normal_analysis(timerResolver.day);
        }
        //周
        if (timerResolver.week.getData().contains("[")) {
            section_analysis(timerResolver.week, timerResolver);
        } else if (timerResolver.week.getData().contains("~")) {
            range_analysis(timerResolver.week, timerResolver);
        } else if (timerResolver.week.getData().contains("/")) {
            interval_analysis(timerResolver.week, timerResolver);
        } else if (mode_exist(timerResolver.week, timerResolver)) {
            mode_analysis(timerResolver.week, timerResolver);
        } else {
            normal_analysis(timerResolver.week);
        }
        //月
        if (timerResolver.month.getData().contains("[")) {
            section_analysis(timerResolver.month, timerResolver);
        } else if (timerResolver.month.getData().contains("~")) {
            range_analysis(timerResolver.month, timerResolver);
        } else if (timerResolver.month.getData().contains("/")) {
            interval_analysis(timerResolver.month, timerResolver);
        } else if (mode_exist(timerResolver.month, timerResolver)) {
            mode_analysis(timerResolver.month, timerResolver);
        } else {
            normal_analysis(timerResolver.month);
        }
        //年
        if (timerResolver.year.getData().contains("[")) {
            section_analysis(timerResolver.year, timerResolver);
        } else if (timerResolver.year.getData().contains("~")) {
            range_analysis(timerResolver.year, timerResolver);
        } else if (timerResolver.year.getData().contains("/")) {
            interval_analysis(timerResolver.year, timerResolver);
        } else if (mode_exist(timerResolver.year, timerResolver)) {
            mode_analysis(timerResolver.year, timerResolver);
        } else {
            normal_analysis(timerResolver.year);
        }

    }


    //分段[]解析
    private static void section_analysis(TimeSlotInfo item, TimerResolverCore timerResolver) throws TimerWheelException {
        //将[]里面的@转换为,号
        item.setData(StringUtil.replaceRangeAll(item.getData(), "@", ",", 0, item.getData().length()));

        List<String> pCharlist = JSON.parseArray(item.getData(), String.class);
        //验证是否都是数字
        for (String s : pCharlist) {
            if (!StringIdentifyUtil.isNumeric(s)) {
                throw new TimerWheelException("[]解析错误非法字符(必须是数字):" + s);
            }
        }
        item.setIndex(0);
        item.setType("[]");
        item.setReturnData(Integer.valueOf(pCharlist.get(0)));

        //因为[xx] 里面全部执行完才算1次,所以执行次数是pCharlist.size() * 执行计划
        if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {
            timerResolver.plan = String.valueOf(pCharlist.size() * Integer.parseInt(timerResolver.plan));
        }
    }

    //区间解析 符号为~ ,位置1表示秒,2表示分支,3表示小时,4表示天,5表示星期,6表示月,7表示年
    private static void range_analysis(TimeSlotInfo item, TimerResolverCore timerResolver) throws TimerWheelException {
        String[] pCharlist = item.getData().split("~");
        //验证是否都是数字
        for (String s : pCharlist) {
            if (!StringIdentifyUtil.isNumeric(s)) {
                throw new TimerWheelException(String.format("~解析错误非法字符(必须是数字):%s\n", s));
            }
        }
        List<String> range = StringUtil.getRange(pCharlist[0], pCharlist[1]);
        String data = JSON.toJSONString(range);
        item.setData(data);
        item.setIndex(0);
        item.setType("~");
        item.setReturnData(Integer.valueOf(range.get(0)));
        //因为范围 里面全部执行完才算1次,所以执行次数是range.size() * 执行计划
        if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {
            timerResolver.plan = String.valueOf(range.size() * Integer.parseInt(timerResolver.plan));
        }
    }


    //间隔/ 解析  (5/2)表示从5开始,每隔2个数 执行一次
    private static void interval_analysis(TimeSlotInfo item, TimerResolverCore timerResolver) throws TimerWheelException {

        String[] pCharlist = item.getData().split("/");
        //验证是否都是数字
        for (String s : pCharlist) {
            if (!StringIdentifyUtil.isNumeric(s)) {
                throw new TimerWheelException(String.format("/解析错误非法字符(必须是数字):%s\n", s));
            }
        }
        String data = "[" + pCharlist[0] + "," + pCharlist[1] + "]";
        item.setData(data);
        item.setIndex(0);
        item.setType("/");
        item.setReturnData(Integer.valueOf(pCharlist[0]));
        //是从什么开始执行的, 这个需要有一次执行, 也就是说 如果执行计划是1,那么就会执行2次
        if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {
            timerResolver.plan = String.valueOf( Integer.parseInt(timerResolver.plan)+1);
        }
    }

    static void mode_analysis(TimeSlotInfo item, TimerResolverCore timerResolver) throws TimerWheelException {
        Set<Map.Entry<String, Object>> entries = timerResolver.mode.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            if (item.getData().startsWith(entry.getKey())) {
                item.setType("mode");
                return;
            }
        }
        throw new TimerWheelException(String.format("mode模式解析错误没有找到对应的模式-请检查模式是否自定义-并且添加到模式集中:%s\n", item));
    }

    //普通解析
    static void normal_analysis(TimeSlotInfo item) throws TimerWheelException {
        if (StringIdentifyUtil.isNumeric(item.getData())) {
            item.setType("number");
            return;
        }
        throw new TimerWheelException(String.format("解析错误,无法解析的表达式:%s\n", item));

    }

    // 获取计划的执行类型(有限还是无限,如果是有限则返回执行次数,如果是无限则返回-1,如果执行计划是0那么返回0(未启动))
    public static int get_plan_type(TimerResolverCore timerResolver) {
        if ("0".equals(timerResolver.plan)) {//未启动
            return 0;
        }
        if ("n".equals(timerResolver.plan)) { //无限
            return -1;
        }
        if ("^n".equals(timerResolver.plan)) {//无限
            return -1;
        }
        if ("^".startsWith(timerResolver.plan)) {//获取阶级的执行次数
            return Integer.parseInt(timerResolver.plan.replace("^", ""));
        }
        if (StringIdentifyUtil.isNumeric(timerResolver.plan)) {//获取执行次数
            return Integer.parseInt(timerResolver.plan);
        }
        return 0;
    }
}
